package com.lyc.servlet.admin;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lyc.dao.SportsmanInfoDAO;
import com.lyc.dao.imp.SportsmanInfoDAOImpl;
import com.lyc.entity.SportsmanInfo;

@WebServlet("/admin/Sportsman-add.let")
public class SportsmanAddServlet extends HttpServlet {

	private SportsmanInfoDAO dao = new SportsmanInfoDAOImpl();
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.reset();
		response.setContentType("application/json;charset=UTF-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        
        try {
        	SportsmanInfo e = new SportsmanInfo();
//        	String code = request.getParameter("sportsmanId");
//        	if(code == null || "".equals(code)) {
//        		throw new ServletException("人员编号不能为空");
//        	}
//        	if(!code.matches("^\\d+$")) {
//        		throw new ServletException("人员编号必须是数字");
//        	}
        	e.setSportsmanId(null);
        	e.setCountryCode(request.getParameter("countryCode"));
        	e.setSportsmanName(request.getParameter("sportsmanName"));
        	e.setSportsmanGender(Integer.parseInt(request.getParameter("sportsmanGender")));
        	e.setSportsmanBrithdate(sdf.parse(request.getParameter("sportsmanBrithdate")));
        	e.setSportsmanHeight(Integer.parseInt(request.getParameter("sportsmanHeight")));
        	e.setSportsmanWeigth(new BigDecimal(request.getParameter("sportsmanWeigth")));
        	e.setSportsmanUri(request.getParameter("sportsmanUri"));
        	e.setSportsmanDesc(request.getParameter("sportsmanDesc"));
            try{
                dao.insert(e);
                response.getWriter().print("{\"ok\":true}");
            }catch (Exception ex){
                throw new ServletException("人员编号不能重复", ex);
            }
        }catch(Exception e) {
        	response.getWriter().print("{\"ok\":false,\"msg\":\""+e.getMessage()+"\"}");
        }
        response.flushBuffer();
	}
}
